package org.itboys.mobile.service.mysql.mobile.product;

import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCollectionService;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_service_mobile
 */
@Service
public class SundayMobileProductService extends BaseMySqlService<SundayProduct> {
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayMobileCollectionService collectionService;
    @Autowired
    private ResourceHolder resourceHolder;

    /**
     * 分页获取商品
     * @param request
     * @param memberId

     */
    public List<SundayProductDto> getProduct(HttpServletRequest request, Long memberId){
        Map<String,Object> param =ParamUtil.packageMysqlPageParam(request,"name");
        param.put("status", SundayCommonConstants.YES);
        List<SundayProduct> products =super.select(param);
        return packageDtoBatch(products);
    }
    /**
     * 分页获取收藏商品
     * @param request
     * @param memberId

     */
    public List<SundayProductDto> getCollectionProduct(HttpServletRequest request, Long memberId){

        Map<String,Object> param =ParamUtil.packageMysqlPageParam(request,"name");
        param.put("status", SundayCommonConstants.YES);
        List<SundayProductDto> dtos =new ArrayList<>();
        List<Long> productIds=collectionService.getCollectionIds(memberId,SundayCommonConstants.collection_type_product);
        if(ListUtil.isNotNull(productIds)){
            param.put("ids",productIds);
            dtos=packageDtoBatch(super.select(param));
        }
        return dtos;
    }

    /**
     * 获取首页推荐商品
     * @param request
     * @return
     */
    public List<SundayProductDto> getRecommendProduct(){
        Map<String,Object> param =new HashMap<String,Object>();
        param.put("status", SundayCommonConstants.YES);
        param.put("isIndexRecommend",SundayCommonConstants.YES);
        return packageDtoBatch(super.select(param));
    }
    /**
     * 查询商品详情
     * @param memberId
     * @param productId
     */
    public SundayProductDto getDetail(Long memberId,Long productId){
        SundayProductDto dto =packageDtoSingle(super.find(productId));

        //组装轮播图
        List<SundayUploadDto> rollImages = uploadService.selectNoPage(dto.getId(),SundayProduct.class.getSimpleName());
        dto.setRollImages(rollImages);
        //商品详情URL
        String descUrl =resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/desc?id="+dto.getId();
        dto.setDescUrl(descUrl);
        //判断受否收藏
        Integer isCollection =collectionService.checkIsCollection(memberId, SundayCommonConstants.collection_type_product,dto.getId());
        dto.setIsCollection(isCollection);
        //加载默认评论
        logger.info("加载评论");
        //List<SundayCommentDto> comments = commentService.getCommentByNumber(2,product.getId(),SundayProduct.class.getSimpleName());
        logger.info("加载评论-结束");
        //List<SundayCommentDto> comments =null;
        //加载推荐商品
        //分享地址
        dto.setShareUrl(resourceHolder.getStringValue("mobileRoot")+"/sunday/mobile/product/share?id="+dto.getId());
        dto.setShareImage(dto.getImage());
        dto.setShareTitle("万品天街-全球购-产品分享");
        dto.setShareDesc(dto.getName()+dto.getViceName()+dto.getStandard());
        //加载关联菜谱
       return  dto;

    }


    /**
     * 组装商品DTO
     * @param products
     * @return
     */
    public List<SundayProductDto> packageDtoBatch(List<SundayProduct> products){;
        List<SundayProductDto> dtos = new ArrayList<SundayProductDto>();
        for(SundayProduct product:products){
            //主图
            dtos.add(packageDtoSingle(product));
        }
       return dtos;
    }
    public SundayProductDto packageDtoSingle(SundayProduct product){
        String imageRoot = resourceHolder.getStringValue("imgRoot");
        return  new SundayProductDto(product.getId(), product.getCreateTime(), product.getCreateDate(),
                product.getName()+product.getStandard(),  product.getViceName(),   product.getStandard(),  product.getMarketPrice(), product.getPrice(),
                imageRoot+product.getImage(),  product.getSaleNumber());
    }

}
